# Methods added to this helper will be available to all templates in the application.

module ApplicationHelper
	include TagsHelper

	def admin?
		logged_in? && current_user.admin?
	end
 
	def avatar(user, options={})
		return image_tag("userpic_2.jpg") if user.nil?
		return link_to image_tag("userpic_2.jpg"), user_path(user) if user.avatar.nil?
		options[:title] = options[:alt] = user.name
		link_to image_tag(user.avatar.public_filename, options), user_path(user)
	end
	
	def logo(community, options={})
		return link_to image_tag("userpic_2.jpg", options), community_posts_path(community) if community.nil? || community.logo[0].nil?
		options[:title] = options[:alt] = community.title
		link_to image_tag(community.logo[0].public_filename(:thumb), options), community_posts_path(community)
	end
	
	def avatar50(user, options={})
		return image_tag("userpic50.jpg", options) if user.nil? || user.avatar.nil?
		options[:title] = options[:alt] = user.name		
		link_to image_tag(user.avatar.public_filename(:content), options), user_path(user)
	end
	
	def pic(avatar, options={})
		return image_tag("userpic_2.jpg") if avatar.nil?
		options[:title] = options[:alt] = 'аватар'
		image_tag(avatar.public_filename, options)
	end

	def text_teaser(txt, length, more_link = "")
	  	cuted = txt[/(.|\n){0,#{length}}[\w]*/]
	  	cuted += " #{more_link}" if(cuted != txt)
	  	Hpricot(cuted).to_html
	  	#truncate_html(txt, length, more_link)
  	end
  
  	def text_teaser2(txt, length, more_link = "")
	  	new_txt = Hpricot(txt).inner_text
	  	cuted = new_txt[/(.|\n){0,#{length}}[\w]*/]
	  	cuted += " #{more_link}" if(cuted != txt)
	  	cuted
  	end
	
	def community_join_link(community, comm=false)
		unless current_user.join_community?(community)
			link_to comm ? "Вступить в коммюнити" : "записаться?", join_community_path(community, :act=>1)
		else
			link_to comm ? "Покинуть коммюнити" :  "отписаться?", join_community_path(community, :act=>0)
		end
	end
	
end
